package com.example.carrierapp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.example.carrierapp.bean.home.SearchCarBean;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆查询适配器.
 */
public class SearchCarAdapter extends BaseAdapter {
    private Context context;

    private List<SearchCarBean> list;

    public SearchCarAdapter(Context context, List<SearchCarBean> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.adapter_searchcar_layout, null);
            viewHolder.image_car = (ImageView) view.findViewById(R.id.image_car);
            viewHolder.listview = (ListView) view.findViewById(R.id.listview);
            viewHolder.gridview = (GridView) view.findViewById(R.id.gridview);
            viewHolder.text_noData = (TextView) view.findViewById(R.id.text_noData);
            view.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        List<SearchCarBean.CarListBean> list1 = new ArrayList<>();//在途车辆
        List<SearchCarBean.CarListBean> list2 = new ArrayList<>();//闲置车辆
        //筛选数据
        for (int m = 0; m < list.get(0).getCarList().size(); m++) {
            if (list.get(0).getCarList().get(m).getState() == 1) {//在途
                list1.add(list.get(0).getCarList().get(m));
            } else if (list.get(0).getCarList().get(m).getState() == 2) {//闲置
                list2.add(list.get(0).getCarList().get(m));
            }
        }
        viewHolder.listview.setAdapter(new DispatchingCarAdapter(list1));
        viewHolder.gridview.setAdapter(new NoDispatchCarAdapter(list2));
        return view;
    }

    class ViewHolder {
        ImageView image_car;//图片
        ListView listview;
        GridView gridview;
        TextView text_noData;
    }

    class DispatchingCarAdapter extends BaseAdapter {//在途车辆适配器
        private List<SearchCarBean.CarListBean> list;

        public DispatchingCarAdapter(List<SearchCarBean.CarListBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder1 viewHolder = null;
            if (view == null) {
                viewHolder = new ViewHolder1();
                view = LayoutInflater.from(context).inflate(R.layout.adapter_dispachingcar_layout, null);
                viewHolder.car_number = (TextView) view.findViewById(R.id.car_number);
                viewHolder.text_juli = (TextView) view.findViewById(R.id.text_juli);
                viewHolder.text_load = (TextView) view.findViewById(R.id.text_load);
                viewHolder.text_unload = (TextView) view.findViewById(R.id.text_unload);
                viewHolder.text_state = (TextView) view.findViewById(R.id.text_state);
                viewHolder.text_carrierName = (TextView) view.findViewById(R.id.text_carrierName);
                viewHolder.text_carrierPhone = (TextView) view.findViewById(R.id.text_carrierPhone);

                view.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder1) view.getTag();
            }
            //车牌
            if (list.get(i).getCarNum() != null && !list.get(i).getCarNum().isEmpty()) {
                viewHolder.car_number.setText(list.get(i).getCarNum());
            } else {
                viewHolder.car_number.setText("--");
            }
            //起始地址
            if (list.get(i).getStartAddress() != null && !list.get(i).getStartAddress().isEmpty()) {
                viewHolder.text_load.setText(list.get(i).getStartAddress());
            } else {
                viewHolder.text_load.setText("--");
            }
            //结束地址
            if (list.get(i).getEndAddress() != null && !list.get(i).getEndAddress().isEmpty()) {
                viewHolder.text_unload.setText(list.get(i).getEndAddress());
            } else {
                viewHolder.text_unload.setText("--");
            }
            //当前调度承运商
            if (list.get(i).getCarrierName() != null && !list.get(i).getCarrierName().isEmpty()) {
                viewHolder.text_carrierName.setText(list.get(i).getCarrierName());
            } else {
                viewHolder.text_carrierName.setText("--");
            }
            //当前调度承运商电话
            if (list.get(i).getCarrierPhone() != null && !list.get(i).getCarrierPhone().isEmpty()) {
                viewHolder.text_carrierPhone.setText(list.get(i).getCarrierPhone());
            } else {
                viewHolder.text_carrierPhone.setText("--");
            }
            if (list.get(i).getLongitude() != null && list.get(i).getLatitude() != null) {//算距离

            }
            return view;
        }

        class ViewHolder1 {
            TextView car_number;
            TextView text_state;
            TextView text_juli;
            TextView text_load;
            TextView text_unload;
            TextView text_carrierName;
            TextView text_carrierPhone;
        }
    }

    class NoDispatchCarAdapter extends BaseAdapter {//闲置车辆适配器
        private List<SearchCarBean.CarListBean> list;

        public NoDispatchCarAdapter(List<SearchCarBean.CarListBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder2 viewHolder = null;
            if (view == null) {
                viewHolder = new ViewHolder2();
                view = LayoutInflater.from(context).inflate(R.layout.adpter_nodispatchcar_layout, null);
                viewHolder.car_number = (TextView) view.findViewById(R.id.car_number);
                viewHolder.text_state = (TextView) view.findViewById(R.id.text_state);
                view.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder2) view.getTag();
            }
            //车牌
            if (list.get(i).getCarNum() != null && !list.get(i).getCarNum().isEmpty()) {
                viewHolder.car_number.setText(list.get(i).getCarNum());
            } else {
                viewHolder.car_number.setText("--");
            }
            return view;
        }

        class ViewHolder2 {
            TextView car_number;
            TextView text_state;
        }
    }
}
